import { useModel } from "umi";
import { useReactive } from "ahooks";

import { FindAllRoles } from "../../_graphql/organization/roles";

const useRoles = () => {
    const { graphqlFetchPlatform } = useModel("global");

    const state = useReactive({
        dataLoading: false,
        roles: [],
    });

    const loadRoles = async () => {
        state.dataLoading = true;
        let res = await graphqlFetchPlatform({
            query: FindAllRoles,
            variables: {},
        });
        state.roles = res?.a?.nodes || [];
        state.dataLoading = false;
    };

    return { ...state, loadRoles };
};

export default useRoles;
